<template>
  <div>
    <el-aside width="240px" class="aside_rolelist">
      <div class="asidelist">
        <div class="hd">
          <div>项目类型</div>
          <div v-if="btnShow">
            <el-button
              class="header-icon el-icon-plus"
              type="primary"
              plain
              @click="dialogVisible=true"
            >添加</el-button>
          </div>
          <div v-else>
              <el-button
              class="header-icon el-icon-search"
              type="primary"
              plain
             @click="getProTypeId()">
            全部</el-button>
          </div>
        </div>
        <div class="role-container" id="rolelist">
          <el-tree :data="dataList" show-checkbox  node-key="id"
              :highlight-current="true"
              ref="tree"
              @node-click="nodebg"
              :expand-on-click-node="false"
              :props="defaultProps">
            <span class="custom-tree-node" slot-scope="{ node, data }" >
              <span class="treefont" >{{ node.label }}</span>
              <span class="bticon" v-show="btnShow">
                <el-dropdown>
                  <i class="el-icon-s-operation"></i>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>
                       <span style="color:#409EFF" class="el-icon-edit" @click="() => edit(data)">编辑</span>
                    </el-dropdown-item>
                    <el-dropdown-item>
                       <span style="color:#F56C6C" class="el-icon-delete" @click="() => remove(node, data)">删除</span>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </span>
            </span>
          </el-tree>
        </div>
      </div>
      <el-dialog :title="title" width="30%" 
                 :visible.sync="dialogVisible"
                 :before-close="handleDialogClose">
        <el-form :model="formData" ref="formData" :rules="rules" label-width="120px">
          <el-form-item label="项目类型名称" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleDialogClose">取 消</el-button>
          <el-button type="primary" @click="addsaveClick('formData')">确 定</el-button>
        </span>
      </el-dialog>
    </el-aside>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //角色列表数据
      rules:{
        name: [{ required: true, message: "类型名称不能为空", trigger: "blur" }],
      },
      dataList: [],
      defaultProps: {
        label: "name"
      },
      dialogVisible: false,
      formData: {
        name: ""
      },
      formeditData:{},
      idx:-1,
      btnShow:false,
      projectType_Id:"",
      title:'添加项目类型'
    };
  },
  created() {
    this.router(this.$route.path)
    this.getData();
  },
  watch:{
    'idx':function(){
      if (this.idx == -1) {
        this.title = '添加项目类型'
      }else{
        this.title = '编辑项目类型'
      }
    }
  },
  methods: {
    nodebg(data) {
        this.$emit('getPTypeId',data.projectTypeId);
    },
    router(val){
      if(val == '/project-manage'){
        this.btnShow = false
      } else if(val == '/project-type'){
        this.btnShow = true
      }
    },
    getData() {
      this.$axios.get('/ProjectType/').then(res =>{
        if (res.data.hasError == false){
          this.dataList = [...res.data.data]
        }
      })
    },
    //添加角色
    addsaveClick(formData) {
      this.$refs[formData].validate(valid =>{
        if (valid) {
          this.formData = { name:this.formData.name }

          if(this.idx == -1){
            this.$axios.post('/ProjectType/',this.formData)
              .then(res =>{
                if (res.data.hasError == false) {
                  this.$message({ type: "success", message: "添加成功!" });
                  this.getData();
                }
              })
          } else{
            this.$axios.put(`/ProjectType/${this.idx}`,this.formData)
              .then(res =>{
                if (res.data.hasError == false) {
                  this.$message({ type: "success", message: "修改成功!" });
                  this.getData();
                  this.formData.name = ""
                  this.idx = -1
                }
              })
          }
          this.dialogVisible = false;
        }
      })
    },
    edit(data) {
      this.dialogVisible = true;
      this.formData.name = data.name;
      this.idx = data.projectTypeId;
    },
    remove(node, data) {
      this.$confirm("是否确认删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
        // type: 'warning'
      }).then(() => {
          this.$axios.delete(`/ProjectType/${data.projectTypeId}`)
            .then(res => {
              if (res.status == 200) {
                this.$message({ type: "success", message: "删除成功!" });
                //刷新
                this.getData();
              }
            });
        })
        .catch(() => {
          this.$message({ type: "info",message: "已取消删除"});
        });
    },
    handleDialogClose(){
      this.idx = -1
      this.formData.name = ''
      this.dialogVisible = false
    },
    getProTypeId(data){
      if(data){
        this.$emit('getPTypeId',data.projectTypeId);
      }else{
        this.$emit('getPTypeId',0);
      }
    }
  }
};
</script>
<style lang="scss" scope>
.header-icon{
  float: none;
}
.asidelist {
  background-color: #fff;
  border-radius: 5px;
  width: 100%;
  height: 100%;
  .hd {
    height: 60px;
    border-bottom: 1px solid #ebeef5;
    box-sizing: border-box;
    padding: 0 10px;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
  }
  .el-tree-node__expand-icon.is-leaf{
    color: f2f2f2;
  }
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 18px;
  }
  .el-checkbox__inner {
    width: 0;
  }
  .el-tree-node__content {
    height: 40px;
  }
 
}
.aside_rolelist {
  height: 100%;
}
</style>